import React, { useEffect, useState, useCallback } from 'react';
import { getCallStrategyDetail } from 'admin/servers/servers';
import { CallStrategyDetail } from 'admin/schema/Task';
import { StrategyItem } from '../../components/StrategyItem';
interface IProps {
  /**
   * 任务Id
   */
  taskId: number;
}
/* 重呼策略-上轮重呼 */
const LastCallStrategy: React.FC<IProps> = (props: IProps) => {
  const [callStrategy, setCallStrategy] = useState<CallStrategyDetail[]>([]);
  const { taskId } = props;
  const loadData = useCallback(() => {
    getCallStrategyDetail(taskId)
      .then(res => {
        setCallStrategy(res.data);
      })
      .catch(error => {
        console.error(`系统异常: ${JSON.stringify(error)}`);
      });
  }, [taskId]);
  useEffect(() => {
    loadData();
  }, [loadData]);

  const len = callStrategy.length;

  return (
    <div className="task-callstrategy">
      {len > 0 && (
        <div key="callStrategy_0" className="box">
          <h4 className="tit">上轮重呼策略</h4>
          <StrategyItem dom={callStrategy[len - 1]} />
        </div>
      )}
      <h4 className="tit">
        当前重呼策略<span>（请至少选择以下一项策略）</span>
      </h4>
    </div>
  );
};

export default LastCallStrategy;
